package cn.fintecher.pangolin.service.manage.request;

import cn.fintecher.pangolin.common.enums.ManagementType;
import cn.fintecher.pangolin.common.enums.OtherTemplateType;
import cn.fintecher.pangolin.common.web.SearchRequest;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Objects;

@Data
public class TemplateSearchRequest extends SearchRequest {

    @ApiModelProperty("委托方ID")
    private String principalId;

    @ApiModelProperty(notes = "模板名称")
    private String templateName;

    @ApiModelProperty(notes = "模板类型")
    private OtherTemplateType type;

    @ApiModelProperty("是否启用")
    private ManagementType isEnabled;


    @Override
    public BoolQueryBuilder generateQueryBuilder() {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if (Objects.nonNull(this.principalId)) {
            queryBuilder.must(QueryBuilders.matchPhraseQuery("principalId.keyword", this.principalId));
        }
        if (Objects.nonNull(this.templateName)) {
            queryBuilder.must(QueryBuilders.matchPhraseQuery("templateName.keyword", this.templateName));
        }
        if (Objects.nonNull(this.type)) {
            queryBuilder.must(QueryBuilders.matchPhraseQuery("type.keyword", this.type.name()));
        }
        if (Objects.nonNull(this.isEnabled)) {
            queryBuilder.must(QueryBuilders.matchPhraseQuery("isEnabled.keyword", this.isEnabled.name()));
        }
        return queryBuilder;
    }
}
